# Maintainer: Peter Budai <peterbudai at hotmail dot com>

_realname=libgphoto2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.5.27
pkgrel=3
pkgdesc="The libgphoto2 camera access and control library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="http://gphoto.org/proj/libgphoto2/"
license=("LGPL")
depends=("${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libgd"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libltdl")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=(https://github.com/gphoto/libgphoto2/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.xz
        libgphoto2-gp_system_filename-fix.patch
        libgphoto2-fix-lumix.patch
	libgphoto2-configure-ws232.patch)
sha256sums=('a7253bd18c6ef65352150ac36a668f883a5ad2634eb6455b3ca9aa6ac98d0073'
            '27c558cdf88bee6ce1f3f564d6dfbc0b78809c33b98d20c9e58e40e335d7b0dd'
            '08b6d5b56d01daae60afa4f0b924ddff2c6d5ef2a5bf6057c601f1efb7019016'
            'fcda293776f18cd506f3aed09b8e0a969c7dbf390574b4b8c3c8377e827dbf8a')
validpgpkeys=('7C4AFD61D8AAE7570796A5172209D6902F969C95') # Marcus Meissner 

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/libgphoto2-gp_system_filename-fix.patch"
  patch -Np1 -i "${srcdir}/libgphoto2-fix-lumix.patch"
  patch -Np1 -i "${srcdir}/libgphoto2-configure-ws232.patch"

  autoreconf -fiv
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  cp -r ${_realname}-${pkgver} build-${MSYSTEM}

  #Note: --with-libusb=no is required for Windows 10 to work.
  cd "${srcdir}/build-${MSYSTEM}"
  ./configure \
    --prefix="${MINGW_PREFIX}" \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-libusb=no \
    --with-gdlib=auto \
    --disable-internal-docs

  # Clang cannot handle '-Wl, -rpath' properly
  find . -depth -name 'config.status' -exec sed -e "s|\${wl}-rpath \${wl}|-L|g" -i {} ';'

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR=${pkgdir} install

  # Remove unused udev helper
  rm -rf "${pkgdir}/${MINGW_PREFIX}/lib/udev"

  # Remove recursive symlink
  rm -rfv "${pkgdir}/${MINGW_PREFIX}/include/gphoto2/gphoto2"  

  # install profile script
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/etc/profile.d

  cat > ${pkgdir}${MINGW_PREFIX}/etc/profile.d/${_realname}.sh <<EOF
export CAMLIBS=${MINGW_PREFIX}/lib/${_realname}/${pkgver}
export IOLIBS=${MINGW_PREFIX}/lib/${_realname}_port/0.12.0
EOF

  install -Dm644 COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
